package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Jieshu;
import model.Leixing;
import model.Tushu;
import model.User;

import org.apache.struts2.ServletActionContext;

import util.Pager;
import util.Util;


import com.opensymphony.xwork2.ActionSupport;

import dao.JieshuDao;
import dao.LeixingDao;
import dao.TushuDao;
import dao.UserDao;

public class ManageAction extends ActionSupport {

	private static final long serialVersionUID = -4304509122548259589L;

	private UserDao userDao;

	private String url = "./";

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}



	//登入请求
	public String login() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String  role = request.getParameter("role");
		User user = userDao.selectBean(" where username = '" + username
				+ "' and password= '" + password + "' and role= "+role);
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			this.setUrl("index.jsp");
			return "redirect";
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");
			response.setContentType("text/html; charset=gbk");
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('用户名或者密码错误');window.location.href='login.jsp';</script>");
		}
		return null;
	}
	//用户退出
	public String loginout() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		this.setUrl("login.jsp");
		return SUCCESS;
	}
	//跳转到修改密码页面
	public String changepwd() {
		this.setUrl("user/user.jsp");
		return SUCCESS;
	}
	//修改密码操作
	public void changepwd2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();

		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("user");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		User bean = userDao.selectBean(" where username= '"+u.getUsername()+"' and password= '"+password1+"'");
		if(bean!=null){
			bean.setPassword(password2);
			userDao.updateBean(bean);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('修改成功');</script>");
		}else{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('用户名或者密码错误');</script>");
		}
	}

	/**
	 * 将leixingDao注入到MangeAction中
	 * @return
	 */
	private LeixingDao leixingDao;


	public LeixingDao getLeixingDao() {
		return leixingDao;
	}

	public void setLeixingDao(LeixingDao leixingDao) {
		this.leixingDao = leixingDao;
	}

	/**
	 * 查看图书类型
	 * @return
	 */
	public String leixinglist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String leixing = request.getParameter("leixing");
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(leixing!=null &&!"".equals(leixing)){
			sb.append("  leixing like '%"+leixing+"%' ");
			sb.append(" and ");
			request.setAttribute("leixing", leixing);
		}


		sb.append(" deletestatus=0 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = leixingDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Leixing> list = leixingDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!leixinglist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("leixing/leixinglist.jsp");
		return "success";

	}

	/**
	 * 添加新图书类型
	 * @return
	 */
	//1 先跳转到增加页面
	public String leixingadd(){
		this.setUrl("leixing/leixingadd.jsp");
		return "success";
	}

	//2 增加新类型并跳转到类型表单页面
	public void leixingadd2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String leixing = request.getParameter("leixing");
		Leixing bean = new Leixing();
		bean.setCreatetime(new Date());
		bean.setDeletestatus(0);
		bean.setLeixing(leixing);
		leixingDao.insertBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('添加成功');window.location.href='method!leixinglist';</script>");
	}

	/**
	 * 删除类型
	 * @throws IOException
	 */
	public void leixingdelete() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id = request.getParameter("id");
		Leixing bean = leixingDao.selectBean(" where id = " + id);
		bean.setDeletestatus(1);
		leixingDao.updateBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('删除成功');window.location.href='method!leixinglist';</script>");
	}



	/**
	 * 将tushuDao注入到MangeAction中
	 */
	private TushuDao tushuDao;

	public TushuDao getTushuDao() {
		return tushuDao;
	}

	public void setTushuDao(TushuDao tushuDao) {
		this.tushuDao = tushuDao;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	/**
	 * 查看图书信息
	 * @return
	 */
	public String tushulist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String shuming = request.getParameter("shuming");
		String zuozhe = request.getParameter("zuozhe");
		String leixing = request.getParameter("leixing");

		request.setAttribute("leixinglist", leixingDao.selectBeanList(0, 9999, " where deletestatus=0 "));

		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(shuming!=null &&!"".equals(shuming)){
			sb.append("  shuming like '%"+shuming+"%' ");
			sb.append(" and ");
			request.setAttribute("shuming", shuming);
		}
		if(zuozhe!=null &&!"".equals(zuozhe)){
			sb.append("  zuozhe like '%"+zuozhe+"%' ");
			sb.append(" and ");
			request.setAttribute("zuozhe", zuozhe);
		}
		if(leixing!=null &&!"".equals(leixing)){
			sb.append("  leixing.leixing like '%"+leixing+"%' ");
			sb.append(" and ");
			request.setAttribute("leixing", leixing);
		}

		sb.append(" deletestatus=0 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = tushuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Tushu> list = tushuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!tushulist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("tushu/tushulist.jsp");
		return "success";

	}


	/**
	 * 查看图书的详细信息
	 * @return
	 */
	public String tushulist2(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		String id = request.getParameter("id");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =" where deletestatus=0 and id=" + id;
		long total = tushuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Tushu> list = tushuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!tushulist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("tushu/tushulist2.jsp");
		return "success";
	}


	/**
	 * 添加新
	 * @return
	 */
	//1 先跳转到增加页面
	public String tushuadd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Leixing> list =(List<Leixing>) leixingDao.selectBeanList(0,9999," where deletestatus = 0");
		request.setAttribute("list", list);
		this.setUrl("tushu/tushuadd.jsp");
		return "success";
	}

	//2 增加新图书并跳转到图书表单页面
	public void tushuadd2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String chubanriqi = request.getParameter("chubanriqi");
		String chubanshe = request.getParameter("chubanshe");
		String jiage = request.getParameter("jiage");
		String bianhao = request.getParameter("bianhao");
		String shuming = request.getParameter("shuming");
		String zongshu = request.getParameter("zongshu");
		String zuozhe = request.getParameter("zuozhe");
		String leixing = request.getParameter("leixing");
		
		if(leixing==null||"".equals(leixing)){
			PrintWriter writer = response.getWriter();
			writer.print("<script language='javascript'>alert('添加失败,图书类型不能为空!');window.location.href='method!tushulist';</script>");
			return;
		}
		
		Tushu bean = new Tushu();
		bean.setChubanriqi(chubanriqi);;
		bean.setChubanshe(chubanshe);
		bean.setBianhao(bianhao);
		bean.setDeletestatus(0);
		bean.setJiage(Double.parseDouble(jiage));
		bean.setLeixing(leixingDao.selectBean(" where id = " + leixing));

		bean.setShuming(shuming);
		bean.setZongshu(Integer.parseInt(zongshu));
		bean.setZuozhe(zuozhe);
		tushuDao.insertBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('添加成功');window.location.href='method!tushulist';</script>");
	}

	/**
	 * 删除图书
	 * @throws IOException
	 */
	public void tushudelete() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id = request.getParameter("id");
		Tushu bean = tushuDao.selectBean(" where id = " + id);
		bean.setDeletestatus(1);
		tushuDao.updateBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('删除成功');window.location.href='method!tushulist';</script>");
	}

	/**
	 * 更新图书信息
	 * @return
	 */
	//首先跳转到更新页面并且找到要更新的图书
	public String tushuupdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Leixing> list =(List<Leixing>) leixingDao.selectBeanList(0,9999," where deletestatus = 0");
		request.setAttribute("list", list);
		String id  = request.getParameter("id");
		Tushu bean = tushuDao.selectBean(" where id = "+id);
		request.setAttribute("bean",bean);	
		this.setUrl("tushu/tushuupdate.jsp");
		return "success";

	}
	//2 更新图书信息并且跳转到图书列表页面
	public void tushuupdate2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id  = request.getParameter("id");
		Tushu bean = tushuDao.selectBean(" where id = "+id);
		String chubanriqi = request.getParameter("chubanriqi");
		String chubanshe = request.getParameter("chubanshe");
		String jiage = request.getParameter("jiage");
		String shuming = request.getParameter("shuming");
		String zongshu = request.getParameter("zongshu");
		String zuozhe = request.getParameter("zuozhe");
		String leixing = request.getParameter("leixing");
		String bianhao = request.getParameter("bianhao");
		bean.setChubanriqi(chubanriqi);
		bean.setChubanshe(chubanshe);
		bean.setDeletestatus(0);
		bean.setJiage(Double.parseDouble(jiage));
		bean.setLeixing(leixingDao.selectBean(" where id = " + leixing));
		bean.setShuming(shuming);
		bean.setZongshu(Integer.parseInt(zongshu));
		bean.setZuozhe(zuozhe);
		bean.setBianhao(bianhao);
		tushuDao.updateBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('更新成功');window.location.href='method!tushulist';</script>");
	}


	/**
	 * 查看读者信息列表
	 */
	public String duzhelist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String username = request.getParameter("username");
		String truename = request.getParameter("truename");
		String status = request.getParameter("status");
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(username!=null &&!"".equals(username)){
			sb.append("  username like '%"+username+"%' ");
			sb.append(" and ");
			request.setAttribute("username", username);
		}
		if(truename!=null &&!"".equals(truename)){
			sb.append("  truename like '%"+truename+"%' ");
			sb.append(" and ");
			request.setAttribute("truename", truename);
		}

		if(status!=null &&!"".equals(status)){
			sb.append("  status like '%"+status+"%' ");
			sb.append(" and ");
			request.setAttribute("status", status);
		}
		sb.append(" role=1 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = userDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<User> list = userDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!duzhelist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("duzhe/duzhelist.jsp");
		return "success";

	}

	/**
	 * 查看读者的详细信息
	 * @return
	 */
	public String duzhelist2(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		String id = request.getParameter("id");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =" where id=" + id;
		long total = userDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<User> list = userDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!duzhelist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("duzhe/duzhelist2.jsp");
		return "success";
	}

	/**
	 * 添加新读者信息
	 * @return
	 */
	//1 先跳转到增加页面
	public String duzheadd(){
		this.setUrl("duzhe/duzheadd.jsp");
		return "success";
	}

	//2 增加新读者并跳转到图书表单页面
	public void duzheadd2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String lianxifangshi = request.getParameter("lianxifangshi");
		String num = request.getParameter("num");
		String truename = request.getParameter("truename");
		String xingbie = request.getParameter("xingbie");
		
		String username = Util.getbianhao(userDao.selectBeanCount(" where role=1 "));
		
		User bean = new User();
		bean.setCreatetime(new Date());
		bean.setStatus(0);
		bean.setLianxifangshi(lianxifangshi);
		bean.setNum(Integer.parseInt(num));
		bean.setPassword("111111");
		bean.setRole(1);
		bean.setTruename(truename);
		bean.setUsername(username);
		bean.setXingbie(xingbie);
		userDao.insertBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('添加成功');window.location.href='method!duzhelist';</script>");
	}

	/**
	 * 
	 * @throws IOException
	 */
	public void duzhetuika() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id = request.getParameter("id");
		User bean = userDao.selectBean(" where id = " + id);
		bean.setStatus(2);
		userDao.updateBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('退卡成功，谢谢您的使用');window.location.href='method!duzhelist';</script>");
	}

	/**
	 * 更新读者信息
	 * @return
	 */
	//首先跳转到更新页面并且找到要更新的图书
	public String duzheupdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id  = request.getParameter("id");
		User bean = userDao.selectBean(" where id = "+id);
		request.setAttribute("bean",bean);	
		this.setUrl("duzhe/duzheupdate.jsp");
		return "success";

	}
	//2 更新图书信息并且跳转到图书列表页面
	public void duzheupdate2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id  = request.getParameter("id");
		User bean = userDao.selectBean(" where id = "+id);
		String lianxifangshi = request.getParameter("lianxifangshi");
		String num = request.getParameter("num");
		String truename = request.getParameter("truename");
		String xingbie = request.getParameter("xingbie");
		String status = request.getParameter("status");
		bean.setNum(Integer.parseInt(num));
		bean.setTruename(truename);
		bean.setXingbie(xingbie);
		bean.setLianxifangshi(lianxifangshi);
		bean.setStatus(Integer.parseInt(status));
		userDao.updateBean(bean);
		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('更新成功');window.location.href='method!duzhelist';</script>");
	}

	/**
	 * 借书管理
	 * 将jieshuDao注入到MangeAction当中
	 */
	private JieshuDao jieshuDao;


	public JieshuDao getJieshuDao() {
		return jieshuDao;
	}

	public void setJieshuDao(JieshuDao jieshuDao) {
		this.jieshuDao = jieshuDao;
	}
	/**
	 * 查询借书信息列表
	 * @return
	 */
	public String jieshulist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String leixing = request.getParameter("leixing");
		String shuming = request.getParameter("shuming");
		request.setAttribute("leixinglist", leixingDao.selectBeanList(0, 9999, " where deletestatus=0 "));
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(shuming!=null &&!"".equals(shuming)){
			sb.append("  shuming like '%"+shuming+"%' ");
			sb.append(" and ");
			request.setAttribute("shuming", shuming);
		}

		if(leixing!=null &&!"".equals(leixing)){
			sb.append("  leixing.leixing like '%"+leixing+"%' ");
			sb.append(" and ");
			request.setAttribute("leixing", leixing);
		}

		sb.append(" deletestatus=0 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = tushuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Tushu> list = tushuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!jieshulist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("jieshu/jieshulist.jsp");
		return "success";

	}







	/**
	 * 借书
	 * @return
	 */
	//首先跳转到借书页面并且找到要借的图书
	public String jieshuupdate(){
		HttpServletRequest request = ServletActionContext.getRequest();

		String id  = request.getParameter("id");
		Tushu bean = tushuDao.selectBean(" where id = "+id);
		request.setAttribute("bean",bean);	
		this.setUrl("jieshu/jieshuupdate.jsp");
		return "success";

	}
	//2 借书
	public void jieshuupdate2() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String tushuid  = request.getParameter("tushuid");
		Tushu tushu = tushuDao.selectBean(" where id= "+tushuid);
		if(tushu.getZongshu()-tushu.getJieshu()<=0){
			PrintWriter writer = response.getWriter();
			writer.print("<script language='javascript'>alert('操作失败，可借图书数量不够！');window.location.href='method!jieshuupdate?id="+tushu.getId()+"';</script>");
			return;
		}
		String username = request.getParameter("username");

		User user =userDao.selectBean(" where username='"+username+"' and status=0 ");

		if(user==null){
			PrintWriter writer = response.getWriter();
			writer.print("<script language='javascript'>alert('操作失败，借书证号不存在或者状态异常！');window.location.href='method!jieshuupdate?id="+tushu.getId()+"';</script>");
			return;
		}

		if(user.getNum()-user.getYijie()<=0){
			PrintWriter writer = response.getWriter();
			writer.print("<script language='javascript'>alert('操作失败，该借书证号已借满，请归还图书后再借书！');window.location.href='method!jieshuupdate?id="+tushu.getId()+"';</script>");
			return;
		}




		Jieshu bean = jieshuDao.selectBean(" where user.id= "+user.getId()+" and tushu.id="+tushu.getId()+"  and jieshustatus='已借书' ");
		if(bean!=null){
			PrintWriter writer = response.getWriter();
			writer.print("<script language='javascript'>alert('操作失败，该借书证号已借该本图书，还未归还');window.location.href='method!jieshuupdate?id="+tushu.getId()+"';</script>");
			return;
		}else{
			bean = new Jieshu();
			bean.setJieshutime(new Date());
			bean.setJieshustatus("已借书");
			bean.setTushu(tushu);
			bean.setUser(user);
			jieshuDao.insertBean(bean);


			tushu.setJieshu(tushu.getJieshu()+1);
			tushuDao.updateBean(tushu);

			user.setYijie(user.getYijie()+1);
			userDao.updateBean(user);
		}




		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('操作成功');window.location.href='method!jieshulist';</script>");
	}



	/**
	 * 还书列表
	 * @return
	 */
	public String huanshulist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		String shuming = request.getParameter("shuming");
		String username = request.getParameter("username");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(shuming!=null &&!"".equals(shuming)){
			sb.append("  tushu.shuming like '%"+shuming+"%' ");
			sb.append(" and ");
			request.setAttribute("shuming", shuming);
		}

		if(username!=null &&!"".equals(username)){
			sb.append("  user.username like '%"+username+"%' ");
			sb.append(" and ");
			request.setAttribute("username", username);
		}

		sb.append(" 1=1 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = jieshuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Jieshu> list = jieshuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!huanshulist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("jieshu/huanshulist.jsp");
		return "success";
	}


	/**
	 * 还书操作
	 * @throws IOException
	 */
	public void huanshudelete() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String id = request.getParameter("id");
		Jieshu bean = jieshuDao.selectBean(" where id = " + id);
		bean.setHuanshutime(new Date());
		bean.setJieshustatus("已还书");
		jieshuDao.updateBean(bean);
		User user = bean.getUser();
		user.setYijie(user.getYijie()-1);
		userDao.updateBean(user);
		Tushu tushu = bean.getTushu();
		tushu.setJieshu(tushu.getJieshu()-1);

		tushuDao.updateBean(tushu);



		PrintWriter writer = response.getWriter();
		writer.print("<script language='javascript'>alert('还书成功');window.location.href='method!huanshulist';</script>");
	}
	/**
	 * 读者角色主要是对图书信息的查询和借阅信息的查询
	 */

	/**
	 * 读者：图书信息查询
	 * @return
	 */
	public String tushulist3(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String shuming = request.getParameter("shuming");
		String zuozhe = request.getParameter("zuozhe");
		String leixing = request.getParameter("leixing");
		request.setAttribute("leixinglist", leixingDao.selectBeanList(0, 9999, " where deletestatus=0 "));
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(shuming!=null &&!"".equals(shuming)){
			sb.append("  shuming like '%"+shuming+"%' ");
			sb.append(" and ");
			request.setAttribute("shuming", shuming);
		}
		if(zuozhe!=null &&!"".equals(zuozhe)){
			sb.append("  zuozhe like '%"+zuozhe+"%' ");
			sb.append(" and ");
			request.setAttribute("zuozhe", zuozhe);
		}
		if(leixing!=null &&!"".equals(leixing)){
			sb.append("  leixing.leixing like '%"+leixing+"%' ");
			sb.append(" and ");
			request.setAttribute("leixing", leixing);
		}

		sb.append(" deletestatus=0 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = tushuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Tushu> list = tushuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!tushulist", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("tushu/tushulist3.jsp");
		return "success";

	}
	/**
	 * 读者：查询借阅信息表单
	 * @return
	 */
	public String huanshulist2(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		String shuming = request.getParameter("shuming");
		String username = request.getParameter("username");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if(shuming!=null &&!"".equals(shuming)){
			sb.append("  tushu.shuming like '%"+shuming+"%' ");
			sb.append(" and ");
			request.setAttribute("shuming", shuming);
		}

		if(username!=null &&!"".equals(username)){
			sb.append("  user.username like '%"+username+"%' ");
			sb.append(" and ");
			request.setAttribute("username", username);
		}

		sb.append(" 1=1 order by id desc ");

		int pagesize = 6;
		int currentpage= 1;
		if(request.getParameter("pagenum") != null){
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		String where =sb.toString();
		long total = jieshuDao.selectBeanCount(where.replaceAll("order by id desc", ""));
		List<Jieshu> list = jieshuDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
		request.setAttribute("list", list);
		String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!huanshulist2", "总共有"+total+"条记录");
		request.setAttribute("pageinfo", pagerinfo);
		this.setUrl("jieshu/huanshulist2.jsp");
		return "success";
	}
}